package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.RoundScore;
import org.example.rowing.vo.RaceVO;
import org.example.rowing.vo.RoundScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ProcessLessSixBoatsService {

    @Autowired
    private RaceProcessCommonService raceProcessCommonService;
    @Autowired
    private RaceInfoService raceInfoService;

    public List<RoundScoreVO> getRank(List<RoundScoreVO> pRounds, RaceVO raceVO) {
        List<RoundScoreVO> list = raceProcessCommonService.sortedEntries(pRounds);
        log.info("排名为：{}", list);
        saveFinalResult(list, raceVO);
        int i = 0;
        for (RoundScoreVO roundScoreVO : list) {
            Double v = Double.valueOf(i++);
            RoundScore roundScore = new RoundScore(raceVO.getId(), "比赛结果", null, null, v, roundScoreVO.getRegistrationVO().getRegistrationId(), roundScoreVO.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        return list;
    }

    public List<RoundScoreVO> getAdvancingToFinals(List<RoundScoreVO> p1List) {
        // 获取晋级决赛的队伍
        return raceProcessCommonService.sortedEntries(p1List);
    }

    private void saveFinalResult(List<RoundScoreVO> finalList, RaceVO raceVO) {
        raceProcessCommonService.saveFinalResult(finalList, raceVO);
    }
}
